// pages/getData/input.js
var utils = require('../../utils/util.js');
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        student_num: [],
        educationArr: [],
        index:3,
        gender: 1,
        educationIndex: 0,
        professionIndex: 0,
        studentStatus:null,
        professionId: null,
        studentEducation: null,
        studentName: null,
        studentMobile: null,
        studentQq: null,
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getStudentNum();
        this.getProfessionEducation();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
    },
    // 检查学生信息
    checkStudentInfo:function () {
        var that=this;
        wx.request({
            url: app.buildUrl('/student/status'),
            header: app.getRequestHeader(),
            method: 'POST',
            success: res => {
                var resp = res.data;
                if (resp.code != 200) {
                    app.alert({"content": resp.msg});
                    return;
                }
                if(resp.data[0].status==1){
                    return
                }
                that.setData({
                    professionId: resp.data[0].professionId,
                    studentEducation:resp.data[0].studentEducation,
                    gender: resp.data[0].gender,
                    studentName: resp.data[0].studentName,
                    studentMobile: resp.data[0].studentMobile,
                    studentQq:resp.data[0].studentQq,
                    professionIndex:utils.getArrIndex(resp.data[0].professionId,that.data.professionIdArr),
                    educationIndex:utils.getArrIndex(resp.data[0].studentEducation,that.data.educationIdArr)
                })
            }
        })
    },
    //获取人数并设置
    getStudentNum: function () {
        var that = this;
        var numArr = utils.getCurrentPageUrlWithArgs().split('=')[1].toString().split('');
        if (numArr.length < 100) {
            numArr.unshift(0, 0);
        }
        that.setData({
            student_num: numArr,
        })
    },
    // 获取职业学历
    getProfessionEducation:function(){
        var that = this;
        wx.request({
            url: app.buildUrl("/enlist/dict"),
            header: app.getRequestHeader(),
            success: function (res) {
                console.log(res);
                var resp = res.data;
                if (resp.code != 200) {
                    app.alert({"content": resp.msg});
                    return;
                }
                var professionArr = [], professionIdArr = [];
                var educationArr = [], educationIdArr = [];
                for (var i = 0, len = resp.data.profession.length; i < len; i++) {
                    professionArr.push(resp.data.profession[i].languageName)
                    professionIdArr.push(resp.data.profession[i].professionId)
                }
                for (var i = 0, len = resp.data.education.length; i < len; i++) {
                    educationArr.push(resp.data.education[len - i - 1].dictLabel)
                    educationIdArr.push(resp.data.education[len - i - 1].dictValue)
                }
                professionArr.unshift('请选择')
                professionIdArr.unshift(0)
                educationArr.unshift('请选择')
                educationIdArr.unshift(0)
                that.setData({
                    educationArr: educationArr,
                    educationIdArr: educationIdArr,
                    professionArr: professionArr,
                    professionIdArr: professionIdArr
                });
                that.checkStudentInfo();
            }
        })
    },
    // 学历选择
    bindEducationChange: function (e) {
        this.setData({
            educationIndex: e.detail.value,
            studentEducation: this.data.educationIdArr[e.detail.value]
        })
    },
    //获取姓名
    getName: function (e) {
        this.setData({
            studentName: e.detail.value
        })
    },
    //性别选择
    radioChange: function (e) {
        this.setData({
            gender: e.detail.value
        })
    },
    //获取手机号
    getMobile: function (e) {
        this.setData({
            studentMobile: e.detail.value
        })
    },
    //获取QQ
    getQQ: function (e) {
        this.setData({
            studentQq: e.detail.value
        })
    },
    // 职业选择
    bindProfessionChange: function (e) {
        this.setData({
            professionIndex: e.detail.value,
            professionId: this.data.professionIdArr[e.detail.value]
        })
    },
    checkData: function (data) {
        if (data.studentName == undefined || data.studentName.trim() == '') {
            app.alert({
                title: '提示',
                content: '请输入您的姓名'
            })
            return false;
        } else if (data.studentEducation == undefined || data.studentEducation == 0) {
            app.alert({
                title: '警告',
                content: '请输入您的学历'
            })
            return false;
            ;
        } else if (data.professionId == undefined || data.professionId == 0) {
            app.alert({
                title: '警告',
                content: '请选择职业'
            })
            return false;
            ;
        } else if (data.studentMobile == undefined || !(/^1[34578]\d{9}$/.test(data.studentMobile))) {
            app.alert({
                title: '警告',
                content: '请输入正确格式的手机号'
            })
            return false;
            ;
        } else if (data.studentQq == undefined || !(/[1-9][0-9]{4,}/.test(data.studentQq))) {
            app.alert({
                title: '警告',
                content: '请输入正确的QQ号码'
            })
            return false;
            ;
        }
        return true
    },
    //报名
    summitEvent: function () {
        var that = this;
        var data = {
            "professionId": this.data.professionId,
            "studentEducation": this.data.studentEducation,
            "gender": this.data.gender,
            "studentName": this.data.studentName,
            "studentMobile": this.data.studentMobile,
            "studentQq": this.data.studentQq
        };
        if (!this.checkData(data)) {
            return;
        }
        wx.showLoading({
            title: '提交中',
        })
        wx.request({
            url: app.buildUrl("/student/enlist"),
            header: app.getRequestHeader(),
            method: 'POST',
            data: data,
            success: function (res) {
                wx.hideLoading()
                var resp = res.data;
                if (resp.code != 200) {
                    app.alert({"content": resp.msg});
                    return;
                }
                wx.showToast({
                    title: '提交成功',
                    icon: 'success',
                    duration: 2000,
                    success: function () {
                        wx.navigateBack()
                    }
                })
            }
        })
    }

})